﻿import mx.controls. *;
import mx.events. *;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import mx.core.IUIComponent;
import mx.events.DragEvent;
import mx.managers.DragManager;
import mx.collections. *;
import mx.managers.PopUpManager;
import mx.containers.TitleWindow;
import flash.geom.Point;
import com.esri.ags.Map;
import com.esri.ags.geometry.Extent;
import com.uprr.app.tnm.model.Model;
import com.uprr.app.tnm.events.AppEvent;

import com.adobe.serialization.json.JSON
	
/**
* bookmark loader
*
* @usage
* @param   event
* @return
*/


private function loadBookmarks():void {

	// disable caching
	var rnd : Number = Math.round(Math.random()*1000);
	var httpRequest:HTTPService =new HTTPService();
		httpRequest.url="MyBookmarksServlet?x="+rnd;
		
	var parameters:Object=new Object();
	parameters.action="load";
	httpRequest.request=parameters;
	httpRequest.resultFormat="text";
	httpRequest.addEventListener(ResultEvent.RESULT, bookmarkLoaderResulteHandler);
	httpRequest.addEventListener("fault", serviceFaultHandler);
	httpRequest.send();
}



/**
 * Handel bookmark loading results
 * 
 * @usage   
 * @param   event 
 * @return  
 */

private function bookmarkLoaderResulteHandler (event : ResultEvent) : void
{
	var rawData : String = String (event.result);
	
	
	try 
	{
		var arr : Array = (JSON.decode (rawData) as Array);
		for (var i : int = 0; i < arr.length; i ++)
		{
			Model.getInstance ().bkmkList.addItem ( 
			{
				bkmkDesc : arr [i].bkmkDesc, mapService:arr[i].mapService, bkmkId : arr [i].bkmkId, minX : arr [i].minX, minY : arr [i].minY, maxX : arr [i].maxX, maxY : arr [i].maxY
			}
			
			
			)
		}
	} catch (err : Error)
	{
		var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when loading bookmarks: "+err.toString();
		errorEvent.dispatch ();
	}
	
}

/*
private var loader:URLLoader;
private function loadBookmarks():void {
			loader= new URLLoader();
			var request:URLRequest = new URLRequest();
			request.method = URLRequestMethod.POST; 
			request.url="MyBookmarksServlet";
			var variables:URLVariables = new URLVariables();
			variables.action = "load"; 
			request.data=variables;
			loader.addEventListener(Event.COMPLETE, bookmarkLoaderResulteHandler);   
			loader.addEventListener(IOErrorEvent.IO_ERROR, serviceFaultHandler);   
			loader.load(request);
}
*/

/**
 * Handel bookmark loading results
 * 
 * @usage   
 * @param   event 
 * @return  
 */
/*
private function bookmarkLoaderResulteHandler (event : Event) : void
{
	var rawData : String = loader.data as String;
	
	try 
	{
		var arr : Array = (JSON.decode (rawData) as Array);
		for (var i : int = 0; i < arr.length; i ++)
		{
			Model.getInstance ().bkmkList.addItem ( 
			{
				bkmkDesc : arr [i].bkmkDesc, bkmkId : arr [i].bkmkId, minX : arr [i].minX, minY : arr [i].minY, maxX : arr [i].maxX, maxY : arr [i].maxY
			}
			
			
			)
		}
	} catch (err : Error)
	{
		var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when loading bookmarks: "+err.toString();
		errorEvent.dispatch ();
	}
	
}
*/

// delete a bookmark
private function removeBookmark (event : Event) : void
{
	var item : Object = bkmkList.selectedItem;
	var idx : int = Model.getInstance ().bkmkList.getItemIndex (item);
	Model.getInstance ().bkmkList.removeItemAt (idx);
	var parameters:Object=new Object();
	parameters.action="delete";
	parameters.bkmkid=item.bkmkId;
	
	var httpRequest:HTTPService = new HTTPService();
	
	var rnd : Number = Math.round(Math.random()*1000);
	httpRequest.url="MyBookmarksServlet?x="+rnd;
	httpRequest.request=parameters;
	httpRequest.resultFormat="text";
	httpRequest.addEventListener(ResultEvent.RESULT, removeBookmarkResultHandler);
	httpRequest.addEventListener("fault", serviceFaultHandler);
	httpRequest.send();
}


private function removeBookmarkResultHandler():void{
	// does nothing, but I have to have this, otherwise the request can not be send out ????
}

// when something goes wrong for all HTTP services
private function serviceFaultHandler (event : Event) : void
{
	var errorEvent : AppEvent = new AppEvent (AppEvent.APP_ERROR);
		errorEvent.data = "Error when loading bookmarks";
		errorEvent.dispatch ();
}

private function bkmk_dragEnter (evt : DragEvent) : void
{
	var obj : IUIComponent = IUIComponent (evt.currentTarget);
	DragManager.acceptDragDrop (obj);
}

/**
 * Bookmark drag-drop remove handler
 * 
 * @usage   
 * @param   evt 
 * @return  
 */

private function bkmk_dragDrop (evt : DragEvent) : void
{
	var item : Object = bkmkList.selectedItem;
	var idx : int = Model.getInstance ().bkmkList.getItemIndex (item);
	Model.getInstance ().bkmkList.removeItemAt (idx);
	
	var parameters:Object=new Object();
	parameters.action="delete";
	parameters.bkmkid=item.bkmkId;
	
	var httpRequest:HTTPService = new HTTPService();
	var rnd : Number = Math.round(Math.random()*1000);
	httpRequest.url = "MyBookmarksServlet?x=" + rnd;
	
	httpRequest.request=parameters;
	httpRequest.resultFormat="text";
	httpRequest.addEventListener("result", removeBookmarkResultHandler);
	httpRequest.addEventListener("fault", serviceFaultHandler);
	httpRequest.send();

}
private var point : Point = new Point ();
// add new bookmark
private function showAddNewBookmarkWin () : void
{
	var addNewBkmkWin : AddNewBookmarkWin = AddNewBookmarkWin (PopUpManager.createPopUp (this, AddNewBookmarkWin , true));
	point.x = addNewBkmk.x;
	point.y = addNewBkmk.y;
	point = addNewBkmk.localToGlobal (point);
	addNewBkmkWin.x = addNewBkmk.x + 300;
	addNewBkmkWin.y = addNewBkmk.y + 150;
	//addNewBkmkWin._map=_map;
	//addNewBkmkWin._bkmkList=bkmkList;
}

// overlay map service click handler
private function itemDoubleClickHandler(event:ListEvent):void {
	//var str:String = new String(event.itemRenderer.data.id);
	var dcEvent : AppEvent = new AppEvent (AppEvent.BKMK_DOUBLECLICK);
	dcEvent.dispatch ();
}

private function dataGridInit(event:FlexEvent):void {
	event.currentTarget.itemRenderer.properties = {doubleClickEnabled:true};
}
